﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace DjBlog.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    public class post : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string act = DjCommon.Utils.GetQueryString("act");
            switch (act)
            {
                case "addcomment":
                    AddComment();
                    break;
                case "addterm":
                    AddTerm();
                    break;
            }
        }

        private void AddTerm()
        {
            object o = HttpContext.Current.Session["loginuser"];
            if (o == null || o.ToString() != "djqq")
            {
                return;
            }

            int ret = -1;
            
            DjModel.TermInfo term = new DjModel.TermInfo();
            term.PostNum = 0;
            term.TermGroup = DjCommon.Utils.GetFormInt("type", 0);
            term.TermName = DjCommon.Utils.GetFormString("value");
            term.TermPos = 0;
            ret = DjBll.Term.AddTerm(term);
            HttpContext.Current.Response.Write("{\"ret\":" + ret + ",\"name\":\""+term.TermName+"\"}");

        }



        /// <summary>
        /// 评论
        /// </summary>
        private void AddComment()
        {
            int ret = -1;
            DjModel.CommentInfo com = new DjModel.CommentInfo();
            com.Author = DjCommon.Utils.GetFormString("author");
            com.CommentDate = DateTime.Now;
            com.CommentStatus = 0;
            com.Content = DjCommon.Utils.GetFormString("comment");
            com.Email = DjCommon.Utils.GetFormString("email");
            com.IP = HttpContext.Current.Request.UserHostAddress;
            com.ParentID = 0;
            com.PostID = DjCommon.Utils.GetInt("postid");
            com.URL = DjCommon.Utils.GetFormString("url");
            ret = DjBll.Post.AddComment(com);
            string s = "{\"ret\":" + ret;
            if (ret == 1)
            {
                s += string.Format(",\"author\":\"{0}\",\"time\":\"{1}\",\"content\":\"{2}\"", com.Author, com.CommentDate.ToString("yyyy-MM-dd"), com.Content);
            }
            s += "}";
            HttpContext.Current.Response.Write(s);
        }
        


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
